package servletAction;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import domain.dao.interfaces.IDAOContactGroup;
import domain.entities.interfaces.IContactGroup;
import actionForm.ActionDispatcherValidationForm;
import actionForm.SearchContactGroupValidationForm;

public class SearchContactGroupAction  extends Action {
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse) {
		final SearchContactGroupValidationForm lForm = (SearchContactGroupValidationForm) pForm;
		
		String part_of_groupname = lForm.getPart_of_groupname();

		try {
			ApplicationContext context = util.SpringUtils.getApplicationContext();
			
			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
			List<IContactGroup> contactGroups = daoContactGroup.searchContactGroup(part_of_groupname);
			pRequest.setAttribute(DomainUtils.SEARCH_CONTACT_GROUP_RESULT, contactGroups);
					

			DomainUtils.displayInfoMessage(
					pRequest, 
					DomainUtils.SHOW_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
					"The research has finished successfully.");
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}
			
			
		}


		DomainUtils.keepParametersContent(pRequest);
		
		return pMapping.findForward("stay");
	}

}
